<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{
            background: #aabccb;
        }
        .input {
            margin-top: 50px;
            margin-left: 500px;
            position: relative;
        }

        #inp {
            width: 300px;
            height: 30px;
            outline: none;
            font-size: 16px;
        }

        #tips {
            color: #999;
            width: 200px;
            height: 20px;
            padding: 0 10px;
            line-height: 20px;
            font-size: 12px;
            text-align: center;
            border: 1px solid #999;

            position: absolute;
            bottom: -30px;
            display: none;
        }

        #tips::before {
            content: "";
            position: absolute;
            top: -17px;
            left: 100px;
            width: 0;
            height: 0;
            border: 8px solid;
            border-color: transparent transparent #999 transparent;
        }

        #tips::after {
            content: "";
            position: absolute;
            top: -14px;
            left: 101px;
            width: 0;
            height: 0;
            border: 7px solid;
            border-color: transparent transparent #fff transparent;
        }
    </style>
</head>

<body>
    <div class="input">
        <input type="text" name="" id="inp">
        <div id="tips"> </div>
    </div>

    <script>
        var inp = document.getElementById("inp");
        var tips = document.getElementById("tips");

        inp.onblur = function () {
            // 控制背景颜色
            if (inp.value == "" || isNaN(inp.value) || inp.value.length != 18) {
                inp.style.background = "#D75455";
            } else {
                inp.style.background = "#00AA90";
            }

            // 控制提示框
            if (inp.value == "") {
                tips.innerText = "内容不能为空";
                tips.style.display = "block";
            } else if (isNaN(inp.value)) {
                tips.innerText = "请输入阿拉伯数字";
                tips.style.display = "block";
            } else if (inp.value.length != 18) {
                tips.innerText = "请输入正确范围内的数字";
                tips.style.display = "block";
            }
        }
        inp.onfocus = function () {
            tips.style.display = "none";
            inp.style.background = "#fff"
        }
    </script>
</body>

</html>